﻿import axios from 'axios';
import router from '../router';
import request from '@/utils/request';

export function fetchClassList() {
  return request({
    url: '/api/teaching/classes',
    method: 'get'
  });
}

export function fetchStudentList(classId, page = 1, pageSize = 10) {
  return request({
    url: '/api/teaching/class-students',
    method: 'get',
    params: {
      classId,
      page,
      pageSize
    }
  });
}

export function fetchStudentListByClassName(className, page = 1, pageSize = 50) {
  return request({
    url: '/api/teaching/students-by-class',
    method: 'get',
    params: {
      className,
      page,
      pageSize
    }
  });
}

export function fetchStudentAnalysis(studentId, subject) {
  return request({
    url: '/api/teaching/student-analysis',
    method: 'get',
    params: {
      studentId,
      subject
    }
  });
}

export function fetchExamRecords(studentId) {
  // 从localStorage获取教师ID
  const teacherId = localStorage.getItem('teacherId');
  
  // 记录API调用信息
  console.log(`调用fetchExamRecords API - studentId: ${studentId}, teacherId: ${teacherId}`);
  
  if (!teacherId) {
    console.error('警告：调用考试记录API时未找到teacherId，API调用可能会失败');
  }
  
  return request({
    url: '/api/teaching/exam-records',
    method: 'get',
    params: {
      studentId,
      teacherId // 显式添加teacherId参数，确保请求中包含此参数
    }
  }).then(response => {
    // 记录API响应状态
    console.log(`获取考试记录API响应成功 - 记录数量: ${response.data?.data?.length || 0}`);
    return response;
  }).catch(error => {
    // 详细记录API调用错误
    console.error('获取考试记录失败:', error);
    console.error('请求参数:', { studentId, teacherId });
    
    // 提供登录状态诊断
    console.log('当前登录状态:', localStorage.getItem('token') ? '已登录' : '未登录');
    
    throw error;
  });
}

export function submitClassAnalysis(analysisData) {
  return request({
    url: '/api/teaching/student-analysis/submit',
    method: 'post',
    data: analysisData
  });
}

export function aiAnalysis(data) {
  return request({
    url: '/api/teaching/ai-analysis',
    method: 'post',
    data
  });
}

export function saveAiAnalysis(data) {
  return request({
    url: '/api/teaching/ai-analysis/save',
    method: 'post',
    data
  });
}
